package com.psa.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import com.psa.element.*;
import com.psa.system.*;

import java.util.List;

/**
 * Created by david on 14-3-18.
 */
public class TaskListAdapter extends BaseAdapter {
    private Context _context=null;
    private LayoutInflater _inflater = null;
    private List<Mission> missionList = null;

    public TaskListAdapter(Context context, List<Mission> data){
        _context = context;
        _inflater = LayoutInflater.from(_context);
        missionList = data;
    }
    @Override
    public int getCount() {
        return missionList==null?0:missionList.size();
    }

    @Override
    public Object getItem(int i) {
        return missionList==null || i<0 || i>=missionList.size()?null:missionList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        HelperClass helperClass = null;
        Mission mission = missionList.get(i);
        if (convertView == null){
            helperClass = new HelperClass();
            convertView = _inflater.inflate(R.layout.task_list_item, null);
            helperClass.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox);
            helperClass.index = i;
            convertView.setTag(helperClass);
        } else {
            helperClass = (HelperClass)convertView.getTag();
        }
        helperClass.checkBox.setText(mission.toString());
        if (mission.isComplete!=helperClass.checkBox.isChecked())
            helperClass.checkBox.setChecked(mission.isComplete);
        return convertView;
    }

    public class HelperClass{
        public CheckBox checkBox;
        public int index;
    }
}
